package ship.weapon;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.EdgeShape;
import com.badlogic.gdx.physics.box2d.Filter;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;

import ship.Ship;
import sun.security.util.Length;

public class Laser implements IWeapon {
	private float max_Radius = 75;
	private final float DefaultPower = 10;
	private Ship ship;
	private Body m_body;
	private BodyDef bodydef;
	private FixtureDef fixturedef;
	private boolean isAction;
	
	public Laser (Ship ship) {
		this.ship = ship;
		isAction = false;
		
		bodydef = new BodyDef();
		bodydef.angle = 3 * MathUtils.PI / 2;
		bodydef.linearDamping = 0.0f;
		bodydef.angularDamping = 0.0f;
		bodydef.allowSleep = true;
		bodydef.active = true;
		
		fixturedef = new FixtureDef();

	}

	@Override
	public void choose() {
		m_body = ship.getBody().getWorld().createBody(bodydef);
	}

	@Override
	public void mouseDown(Vector2 point) {
		//System.out.println("Weapon down");
	}

	@Override
	public void mouseUp(Vector2 point) {
		//System.out.println("Weapon up");
		if (ship.getBody().getFixtureList().size() > 1) {
			ship.getBody().destroyFixture(ship.getBody().getFixtureList().get(1));
		}
	}
	
	@Override
	public void mouseMoving(Vector2 point) {
		//System.out.println("Weapon move");
		
		
		//point = point.add(ship.getBody().getWorldCenter().rotate(180));
		
		//ship.getBody().getWorldCenter().rotate(180));
		//ship.getBody().getLocalCenter().rotate(180)
		
		float L = 1;
		
		Vector2 vec;
		float angle = point.angle() * MathUtils.degreesToRadians, 
				cos = MathUtils.cos(angle), 
				sin = MathUtils.sin(angle);
		//if (point.len() > max_Radius) {
		//	vec = new Vector2(max_Radius * cos, max_Radius * sin);
		//} else {
			vec = point;
		//}
		//System.out.println("angle " + point.angle());
		//System.out.println(-L * sin + " " + L * cos);
		//System.out.println(vec.x + " " + vec.y);
		//System.out.println(L * sin + " " + -L * cos);
		
		PolygonShape shape = new PolygonShape();
		Vector2[] vertices = new Vector2[3];
		vertices[2] = new Vector2(-L * sin, L * cos);
		vertices[1] = new Vector2(vec.x, vec.y);
		vertices[0] = new Vector2(L * sin, -L * cos);		
		if (ship.getBody().getFixtureList().size() > 1) {
			ship.getBody().destroyFixture(ship.getBody().getFixtureList().get(1));
		}
		shape.set(vertices);
		fixturedef = new FixtureDef();
		fixturedef.isSensor = true;
		fixturedef.shape = shape;
		//Filter
		ship.getBody().createFixture(fixturedef);
	}

	@Override
	public void unchoose() {
		//m_body.getWorld().destroyBody(m_body);
	}

	private void addFixtures() {
		
	}


}
